//
//  CXHViewController.m
//  CXHAdSDK-UBiMAX
//
//  Created by 二跳 on 07/27/2025.
//  Copyright (c) 2025 二跳. All rights reserved.
//

#import "CXHViewController.h"
#import <UBiMAXInterstitial/UBiMAXInterstitial-umbrella.h>
#import <UBiMAXNative/UBiMAXNative-umbrella.h>
#import <CXHAdSDK-Kit/CXHUtilsKit.h>
#import <UBiMAXRewardedVideo/UBiMAXRewardedVideo-umbrella.h>


@interface CXHViewController ()<UMTInterstitialLoadDelegate,UMTInterstitialShowDelegate,UMTNativeLoadDelegate,UMTNativeDelegate,UMTRewardedVideoLoadDelegate,UMTRewardedVideoShowDelegate>

@property(nonatomic, strong)UMTInterstitial *interstitialAd;
@property(nonatomic, strong)UMTNative *nativeAd;
@property(nonatomic, strong)UMTRewardedVideo *rewardedVideoAd;
@property(nonatomic, strong)UMTNativeAdView *adView;

@end

@implementation CXHViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        [self loadInter];
//        [self loadNative];
        [self loadReward];
    });
}
#pragma mark - load reward ad

- (void)loadReward{
    
    UMTRewardedVideo *ad = [[UMTRewardedVideo alloc] initWithSlotId:@"1001070"];
    self.rewardedVideoAd = ad;
    [[UMTAdManager sharedManager] loadRewardedVideo:ad extra:@{} delegate:self];
}

#pragma mark - UMTRewardedVideoLoadDelegate
/// 激励视频加载成功
- (void)umtRewardedVideo:(UMTRewardedVideo *)rewardedVideoAd didLoadWithExtra:(NSDictionary *)extra{
    NSLog(@"%s", __func__);
    if ([[UMTAdManager sharedManager] isReadyForRewardedVideo:_rewardedVideoAd]) {
      [[UMTAdManager sharedManager] showRewardedVideo:_rewardedVideoAd inRootVC:self delegate:self];
    }
}
/// 激励视频加载失败
- (void)umtRewardedVideo:(UMTRewardedVideo *)rewardedVideoAd didLoadFailed:(UMTError *)error{
    NSLog(@"%s", __func__);
}

#pragma mark - UMTRewardedVideoShowDelegate
/// 激励视频展示
- (void)umtRewardedVideo:(UMTRewardedVideo *)rewardedVideoAd didShowWithExtra:(NSDictionary *)extra{
    NSLog(@"%s", __func__);
}
/// 激励视频展示失败
- (void)umtRewardedVideo:(UMTRewardedVideo *)rewardedVideoAd didShowFailed:(UMTError *)error{
    NSLog(@"%s", __func__);
}
/// 激励视频点击
- (void)umtRewardedVideo:(UMTRewardedVideo *)rewardedVideoAd didClickWithExtra:(NSDictionary *)extra{
    NSLog(@"%s", __func__);
}
/// 激励视频关闭
- (void)umtRewardedVideo:(UMTRewardedVideo *)rewardedVideoAd didCloseWithExtra:(NSDictionary *)extra{
    NSLog(@"%s", __func__);
}
/// 激励视频 奖励发放
- (void)umtRewardedVideo:(UMTRewardedVideo *)rewardedVideoAd didRewardedWithExtra:(NSDictionary *)extra{
    NSLog(@"%s", __func__);
}
/// 激励视频 播放状态变更
- (void)umtRewardedVideo:(UMTRewardedVideo *)rewardedVideoAd didChangedPlayStatus:(UMTVideoPlayerStatus)status{
    NSLog(@"%s", __func__);
}
/// 激励视频播放完成或异常
- (void)umtRewardedVideo:(UMTRewardedVideo *)rewardedVideoAd didPlayFinishWithExtra:(NSDictionary *)extra failed:(UMTError *)error{
    NSLog(@"%s", __func__);
}

#pragma mark - load native ad

- (void)loadNative{
    
    UMTNative *nativeAd = [[UMTNative alloc] initWithSlotId:@"1001071"];
    self.nativeAd = nativeAd;
    // 信息流广告大小， 模版广告以实际渲染大小为准
    nativeAd.size = CGSizeMake(kCXHScreenWidth, 50);

    // 部分源，需要在load前传入 展示所用的 rootViewController
    [[UMTAdManager sharedManager] loadNativeAd:nativeAd extra:@{kUMTRootViewController: self} delegate:self];
     
}

#pragma mark - UMTNativeLoadDelegate
/// 信息流加载成功
- (void)umtNativeAd:(UMTNative *)native didLoadWithAds:(NSArray <UMTNativeOffer*> *)ads{
    NSLog(@"%s", __func__);
    for (UMTNativeOffer *offer in ads) {
        if (!offer.isExpress) {
            continue;
        }
        offer.delegate = self;
        [offer renderAdView:offer.canvasView selfRenderView:nil];
        self.adView  = offer.canvasView;
        CGSize size = offer.canvasView.frame.size;
        self.adView.frame = CGRectMake(0, 300, kCXHScreenWidth, size.height);
        [self.view addSubview:self.adView];
    }
    
}

/// 信息流加载失败
- (void)umtNativeAd:(UMTNative *)native didLoadFailed:(UMTError *)error{
    NSLog(@"%s", __func__);
}

#pragma mark - UMTNativeDelegate
//// 信息流展示
- (void)umtNativeAd:(UMTNative *)native didShowSucc:(UMTNativeOffer *)offer{
    NSLog(@"%s", __func__);
}
//// 信息流渲染成功，仅模板广告，可能不回调
- (void)umtNativeAd:(UMTNative *)native didAdViewRenderSucc:(UMTNativeOffer *)offer{
    NSLog(@"%s", __func__);
}
/// 信息流渲染失败，仅模板广告，可能不回调
- (void)umtNativeAd:(UMTNative *)native didAdView:(UMTNativeOffer *)offer renderFailed:(UMTError *)error{
    NSLog(@"%s", __func__);
}
/// 信息流展示失败，仅支持 baidu
- (void)umtNativeAd:(UMTNative *)native didShow:(UMTNativeOffer *)offer failed:(UMTError *)error{
    NSLog(@"%s", __func__);
}
/// 信息流点击
- (void)umtNativeAd:(UMTNative *)native didClick:(UMTNativeOffer *)offer{
    NSLog(@"%s", __func__);
}
/// 信息流关闭
- (void)umtNativeAd:(UMTNative *)native didClose:(UMTNativeOffer *)offer{
    NSLog(@"%s", __func__);
}
/// 信息流详情关闭，
- (void)umtNativeAd:(UMTNative *)native didDetailClose:(UMTNativeOffer *)offer{
    NSLog(@"%s", __func__);
}
/// 信息流，视频播放状态变更
- (void)umtNativeAd:(UMTNative *)native offer:(UMTNativeOffer *)offer didChangedPlayStatus:(UMTVideoPlayerStatus)status{
    NSLog(@"%s", __func__);
}
/// 信息流，视频播放完成或异常
- (void)umtNativeAd:(UMTNative *)native offer:(UMTNativeOffer *)offer didPlayFinishExtra:(NSDictionary *)extra failed:(UMTError *)error{
    NSLog(@"%s", __func__);
}
/// 信息流，视频播放进度, 仅支持 Gdt，UBiMAXAdx
///  @param time 播放进度，单位： ms
///  @param duration 视频时长，单位：ms
- (void)umtNativeAd:(UMTNative *)native offer:(UMTNativeOffer *)offer didChangedPlayTime:(CGFloat)time duration:(CGFloat)duration extra:(NSDictionary *)extra{
    NSLog(@"%s", __func__);
}

#pragma mark - load inter ad
// 加载插屏
- (void)loadInter{
    UMTInterstitial *interstitialAd = [[UMTInterstitial alloc] initWithSlotId:@"1001069"];
    self.interstitialAd = interstitialAd;
    NSMutableDictionary *extra = [NSMutableDictionary dictionary];
    if (self) {
        [extra setValue:self forKey:kUMTRootViewController];
    }
    [[UMTAdManager sharedManager] loadInterstitial:interstitialAd extra:extra delegate:self];
     
}

#pragma mark - UMTInterstitialLoadDelegate
/// 插屏加载成功
- (void)umtInterstitial:(UMTInterstitial *)interstitialAd didLoadWithExtra:(NSDictionary *)extra{
    NSLog(@"%s", __func__);
    
    if ([[UMTAdManager sharedManager] isReadyForInterstitial:self.interstitialAd]) {
      [[UMTAdManager sharedManager] showInterstitial:_interstitialAd inRootVC:self delegate:self];
    }
}
/// 插屏加载失败
- (void)umtInterstitial:(UMTInterstitial *)interstitialAd didLoadFailed:(UMTError *)error{
    NSLog(@"%s", __func__);
}

/// 插屏展示
- (void)umtInterstitial:(UMTInterstitial *)interstitialAd didShowWithExtra:(NSDictionary *)extra{
    NSLog(@"%s", __func__);
}
/// 插屏展示失败
- (void)umtInterstitial:(UMTInterstitial *)interstitialAd didShowFailed:(UMTError *)error{
    NSLog(@"%s", __func__);
}
/// 插屏点击
- (void)umtInterstitial:(UMTInterstitial *)interstitialAd didClickWithExtra:(NSDictionary *)extra{
    NSLog(@"%s", __func__);
}
/// 插屏关闭
- (void)umtInterstitial:(UMTInterstitial *)interstitialAd didCloseWithExtra:(NSDictionary *)extra{
    NSLog(@"%s", __func__);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
